﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MvcMall.AdminWeb.DtoModels;
using MvcMall.Core.Contacts.Tasks;
using MvcMall.Core.Enums;
using MvcMall.Framework.UnitOfWork;

namespace MvcMall.AdminWeb.Manage.GoodsBrands
{
    public partial class DSList : ManageBasePage
    {
        private IGoodsBrandTask _goodsBrandTask;
        protected void Page_Load(object sender, EventArgs e)
        {
            ValidatePagePower("GoodsBrandIndex");

            _goodsBrandTask = GetInstance<IGoodsBrandTask>();

            if (!IsPostBack)
            {
                BindRtp();
            }
        }

        private void BindRtp()
        {
            var list = _goodsBrandTask.GetPagedList(ANPager.CurrentPageIndex, ANPager.PageSize, GoodsBrandStatus.未审核);
            var dtoList = list.Select(c => new GoodsBrandDto
                                               {
                                                   AdminUserName =
                                                       c.AdminUser == null ? string.Empty : c.AdminUser.UserName,
                                                   BrandName = c.BrandName,
                                                   CatName = c.GoodsCat.CatName,
                                                   CreateTime = c.CreateTime,
                                                   GoodsBrandStatus = c.GoodsBrandStatus,
                                                   Id = c.Id,
                                                   ModifyTime = c.ModifyTime,
                                                   SortOrder = c.SortOrder,
                                                   UserName = c.Store == null ? string.Empty : c.Store.User.UserName
                                               }).ToList();
            rptBrands.DataSource = dtoList;
            rptBrands.DataBind();
            ANPager.RecordCount = list.TotalItemCount;
        }

        protected void ANPager_PageChanged(object sender, EventArgs e)
        {
            BindRtp();
        }

        protected void rptBrands_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {

                var lblId = e.Item.FindControl("lblId") as Label;
                var id = Convert.ToInt32(lblId.Text);

                switch (e.CommandName)
                {
                    case "ShenHe":
                        using (var work = UnitOfWork.Begin())
                        {
                            if (_goodsBrandTask.ShenHe(LogOnUser, id) != null)
                            {
                                work.Commit();
                                ShowMsg("审核成功");
                            }
                            else
                            {
                                ShowMsg("审核失败");
                            }
                        }
                        break;
                    case "Deny":
                        using (var work = UnitOfWork.Begin())
                        {
                            if (_goodsBrandTask.Deny(LogOnUser, id) != null)
                            {
                                work.Commit();
                                ShowMsg("拒绝成功");
                            }

                            else
                            {
                                ShowMsg("拒绝失败");
                            }
                        }
                        break;
                }


                BindRtp();
            }
        }
    }
}